import * as Blockly from 'blockly/core';
import { Msg } from 'blockly/core';
import { BLOCK_HUE } from './config';

export const mixly_lib_fs_begin = {
    init: function () {
        this.appendDummyInput()
            .setAlign(Blockly.ALIGN_LEFT)
            .appendField(new Blockly.FieldDropdown([
                ['LittleFS', 'LittleFS'],
                ['SPIFFS', 'SPIFFS']
            ]), 'FS')
            .appendField(new Blockly.FieldDropdown([
                [Msg.MIXLY_LIB_FS_MOUNT, 'begin'],
                [Msg.MIXLY_LIB_FS_UNMOUNT, 'end'],
                [Msg.MIXLY_LIB_FS_FORMAT, 'format']
            ]), 'TYPE');
        this.setInputsInline(true);
        this.setOutput(true, null);
        this.setColour(BLOCK_HUE);
        this.setTooltip('');
        this.setHelpUrl('');
    }
};

export const mixly_lib_fs_open = {
    init: function () {
        this.appendValueInput('PATH')
            .setCheck(null)
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField(new Blockly.FieldDropdown([
                ['LittleFS', 'LittleFS'],
                ['SPIFFS', 'SPIFFS']
            ]), 'FS')
            .appendField(Msg.MIXLY_LIB_FS_OPEN_FILE);
        this.appendDummyInput()
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField(Msg.MIXLY_LIB_FS_MODE)
            .appendField(new Blockly.FieldDropdown([
                [Msg.MIXLY_LIB_FS_MODE_READ + ' (r)', 'r'],
                [Msg.MIXLY_LIB_FS_MODE_READ_WRITE + ' (r+)', 'r+'],
                [Msg.MIXLY_LIB_FS_MODE_WRITE + ' (w)', 'w'],
                [Msg.MIXLY_LIB_FS_MODE_READ_WRITE + ' (w+)', 'w+'],
                [Msg.MIXLY_LIB_FS_MODE_APPEND + ' (a)', 'a'],
                [`${Msg.MIXLY_LIB_FS_MODE_READ_WRITE}+${Msg.MIXLY_LIB_FS_MODE_APPEND} (a+)`, 'a+']
            ]), 'TYPE');
        this.setInputsInline(true);
        this.setOutput(true, null);
        this.setColour(BLOCK_HUE);
        this.setTooltip('');
        this.setHelpUrl('');
    }
};

export const mixly_lib_fs_open_dir = {
    init: function () {
        this.appendValueInput('PATH')
            .setCheck(null)
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField(new Blockly.FieldDropdown([
                ['LittleFS', 'LittleFS'],
                ['SPIFFS', 'SPIFFS']
            ]), 'FS')
            .appendField(Msg.MIXLY_LIB_FS_OPEN_DIR);
        this.setInputsInline(true);
        this.setOutput(true, null);
        this.setColour(BLOCK_HUE);
        this.setTooltip('');
        this.setHelpUrl('');
    }
};

export const mixly_lib_fs_exists = {
    init: function () {
        this.appendValueInput('PATH')
            .setCheck(null)
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField(new Blockly.FieldDropdown([
                ['LittleFS', 'LittleFS'],
                ['SPIFFS', 'SPIFFS']
            ]), 'FS')
            .appendField(Msg.MIXLY_LIB_FS_PATH);
        this.appendDummyInput()
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField(Msg.MIXLY_LIB_FS_EXISTS);
        this.setInputsInline(true);
        this.setOutput(true, null);
        this.setColour(BLOCK_HUE);
        this.setTooltip('');
        this.setHelpUrl('');
    }
};

export const mixly_lib_fs_op_dir = {
    init: function () {
        this.appendValueInput('PATH')
            .setCheck(null)
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField(new Blockly.FieldDropdown([
                ['LittleFS', 'LittleFS'],
                ['SPIFFS', 'SPIFFS']
            ]), 'FS')
            .appendField(new Blockly.FieldDropdown([
                [Msg.MIXLY_LIB_FS_NEW_DIR, 'mkdir'],
                [Msg.MIXLY_LIB_FS_DEL_DIR, 'rmdir']
            ]), 'TYPE');
        this.setInputsInline(true);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setColour(BLOCK_HUE);
        this.setTooltip('');
        this.setHelpUrl('');
    }
};

export const mixly_lib_fs_remove = {
    init: function () {
        this.appendValueInput('PATH')
            .setCheck(null)
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField(new Blockly.FieldDropdown([
                ['LittleFS', 'LittleFS'],
                ['SPIFFS', 'SPIFFS']
            ]), 'FS')
            .appendField(Msg.MIXLY_LIB_FS_DEL_FILE);
        this.setInputsInline(true);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setColour(BLOCK_HUE);
        this.setTooltip('');
        this.setHelpUrl('');
    }
};

export const mixly_lib_fs_rename = {
    init: function () {
        this.appendValueInput('PATH1')
            .setCheck(null)
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField(new Blockly.FieldDropdown([
                ['LittleFS', 'LittleFS'],
                ['SPIFFS', 'SPIFFS']
            ]), 'FS')
            .appendField(Msg.MIXLY_LIB_FS_RENAME);
        this.appendValueInput('PATH2')
            .setCheck(null)
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField(Msg.MIXLY_LIB_FS_TO);
        this.setInputsInline(true);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setColour(BLOCK_HUE);
        this.setTooltip('');
        this.setHelpUrl('');
    }
};

export const mixly_lib_fs_while_next = {
    init: function () {
        this.appendValueInput('DIR')
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField(Msg.MIXLY_LIB_FS_FOR_DIR);
        this.appendDummyInput()
            .appendField(Msg.MIXLY_LIB_FS_EACH_ITEM);
        this.appendStatementInput('DO')
            .appendField(Msg.MIXLY_LIB_FS_DO);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setInputsInline(true);
        this.setColour(BLOCK_HUE);
        this.setTooltip('');
    }
};

export const mixly_lib_fs_get_dir_info = {
    init: function () {
        this.appendValueInput('DIR')
            .setCheck(null)
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField('FS')
            .appendField(Msg.MIXLY_LIB_FS_GET_DIR);
        this.appendDummyInput()
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField(Msg.MIXLY_LIB_FS_ITEM_REFERRED_TO)
            .appendField(new Blockly.FieldDropdown([
                [Msg.MIXLY_LIB_FS_NAME, 'fileName'],
                [Msg.MIXLY_LIB_FS_SIZE, 'fileSize'],
                [Msg.MIXLY_LIB_FS_FILE_TIME, 'fileTime']
            ]), 'TYPE');
        this.setInputsInline(true);
        this.setOutput(true, null);
        this.setColour(BLOCK_HUE);
        this.setTooltip('');
        this.setHelpUrl('');
    }
};

export const mixly_lib_fs_open_file = {
    init: function () {
        this.appendValueInput('DIR')
            .setCheck(null)
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField('FS')
            .appendField(Msg.MIXLY_LIB_FS_OPEN_DIR);
        this.appendDummyInput()
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField(Msg.MIXLY_LIB_FS_ITEM_REFERRED_TO)
            .appendField(Msg.MIXLY_LIB_FS_MODE)
            .appendField(new Blockly.FieldDropdown([
                [Msg.MIXLY_LIB_FS_MODE_READ + ' (r)', 'r'],
                [Msg.MIXLY_LIB_FS_MODE_READ_WRITE + ' (r+)', 'r+'],
                [Msg.MIXLY_LIB_FS_MODE_WRITE + ' (w)', 'w'],
                [Msg.MIXLY_LIB_FS_MODE_READ_WRITE + ' (w+)', 'w+'],
                [Msg.MIXLY_LIB_FS_MODE_APPEND + ' (a)', 'a'],
                [`${Msg.MIXLY_LIB_FS_MODE_READ_WRITE}+${Msg.MIXLY_LIB_FS_MODE_APPEND} (a+)`, 'a+']
            ]), 'TYPE');
        this.setInputsInline(true);
        this.setOutput(true, null);
        this.setColour(BLOCK_HUE);
        this.setTooltip('');
        this.setHelpUrl('');
    }
};

export const mixly_lib_fs_get_file_info = {
    init: function () {
        this.appendValueInput('FILE')
            .setCheck(null)
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField('FS')
            .appendField(Msg.MIXLY_LIB_FS_GET_FILE);
        this.appendDummyInput()
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField(new Blockly.FieldDropdown([
                [Msg.MIXLY_LIB_FS_NAME, 'name'],
                [Msg.MIXLY_LIB_FS_PATH, 'fullName'],
                [Msg.MIXLY_LIB_FS_SIZE, 'size'],
                [Msg.MIXLY_LIB_FS_LAST_WRITE_TIME, 'getLastWrite'],
                [Msg.MIXLY_LIB_FS_CREATION_TIME, 'getCreationTime'],
                [Msg.MIXLY_LIB_FS_FILE_POSITION, 'position']
            ]), 'TYPE');
        this.setInputsInline(true);
        this.setOutput(true, null);
        this.setColour(BLOCK_HUE);
        this.setTooltip('');
        this.setHelpUrl('');
    }
};

export const mixly_lib_fs_stat = {
    init: function () {
        this.appendValueInput('DIR')
            .setCheck(null)
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField('FS');
        this.appendDummyInput()
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField(Msg.MIXLY_LIB_FS_IS)
            .appendField(new Blockly.FieldDropdown([
                [Msg.MIXLY_LIB_FS_FILE + '?', 'isFile'],
                [Msg.MIXLY_LIB_FS_DIR + '?', 'isDirectory']
            ]), 'TYPE');
        this.setInputsInline(true);
        this.setOutput(true, null);
        this.setColour(BLOCK_HUE);
        this.setTooltip('');
        this.setHelpUrl('');
    }
};

export const mixly_lib_fs_file_read = {
    init: function () {
        this.appendValueInput('FILE')
            .setCheck(null)
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField('FS')
            .appendField(Msg.MIXLY_LIB_FS_FILE);
        this.appendDummyInput()
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField(new Blockly.FieldDropdown([
                ['read', 'read'],
                ['peek', 'peek'],
                ['parseInt', 'parseInt'],
                ['parseFloat', 'parseFloat']
            ]), 'TYPE');
        this.setInputsInline(true);
        this.setOutput(true, null);
        this.setColour(BLOCK_HUE);
        this.setTooltip('');
        this.setHelpUrl('');
    }
};

export const mixly_lib_fs_file_read_string = {
    init: function () {
        this.appendValueInput('FILE')
            .setCheck(null)
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField('FS')
            .appendField(Msg.MIXLY_LIB_FS_FILE);
        this.appendDummyInput()
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField(Msg.MIXLY_SERIAL_READSTR);
        this.setInputsInline(true);
        this.setOutput(true, null);
        this.setColour(BLOCK_HUE);
        this.setTooltip('');
        this.setHelpUrl('');
    }
};

export const mixly_lib_fs_file_read_string_until = {
    init: function () {
        this.appendValueInput('FILE')
            .setCheck(null)
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField('FS')
            .appendField(Msg.MIXLY_LIB_FS_FILE);
        this.appendDummyInput()
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField(Msg.MIXLY_SERIAL_READSTR);
        this.appendValueInput('UNTIL')
            .setCheck(null)
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField(Msg.CONTROLS_OPERATOR_UNTIL);
        this.setInputsInline(true);
        this.setOutput(true, null);
        this.setColour(BLOCK_HUE);
        this.setTooltip('');
        this.setHelpUrl('');
    }
};

export const mixly_lib_fs_file_print = {
    init: function () {
        this.appendValueInput('FILE')
            .setCheck(null)
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField('FS')
            .appendField(Msg.MIXLY_LIB_FS_FILE);
        this.appendValueInput('VALUE')
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField(Msg.MIXLY_LIB_FS_WRITE_STRING)
            .appendField(new Blockly.FieldDropdown([
                [Msg.TEXT_PRINT_Huanhang_TOOLTIP, 'println'],
                [Msg.MIXLY_PRINT_INLINE, 'print']
            ]), 'TYPE');
        this.setInputsInline(true);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setColour(BLOCK_HUE);
        this.setTooltip('');
        this.setHelpUrl('');
    }
};

export const mixly_lib_fs_file_close = {
    init: function () {
        this.appendValueInput('FILE')
            .setCheck(null)
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField('FS')
            .appendField(Msg.MIXLY_MICROBIT_PY_STORAGE_CLOSE_FILE);
        this.setInputsInline(true);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setColour(BLOCK_HUE);
        this.setTooltip('');
        this.setHelpUrl('');
    }
};